package tutor;

import java.util.Vector;
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;

/**
 * @author y_plaksyuk
 */
public class TutorApp extends MIDlet implements CommandListener {

	private boolean midletPaused = false;
    private Command exitCommand;
    private List form;
	private Command nextCommand;
	private Command cancelCommand;

	private Vector units;

	/**
	 * The TutorApp constructor.
	 */
	public TutorApp() {
	}

    /**
     * Initilizes the application.
     * It is called only once when the MIDlet is started. The method is called before the <code>startMIDlet</code> method.
     */
    private void initialize() {
		units = new Vector();
    }

    /**
     * Performs an action assigned to the Mobile Device - MIDlet Started point.
     */
    public void startMIDlet() {
        switchDisplayable(null, getMainScreen());
    }

    /**
     * Performs an action assigned to the Mobile Device - MIDlet Resumed point.
     */
    public void resumeMIDlet() {
	}

    /**
     * Switches a current displayable in a display. The <code>display</code> instance is taken from <code>getDisplay</code> method. This method is used by all actions in the design for switching displayable.
     * @param alert the Alert which is temporarily set to the display; if <code>null</code>, then <code>nextDisplayable</code> is set immediately
     * @param nextDisplayable the Displayable to be set
     */
    public void switchDisplayable(Alert alert, Displayable nextDisplayable) {
        Display display = getDisplay();
        if (alert == null) {
            display.setCurrent(nextDisplayable);
        }
		else {
            display.setCurrent(alert, nextDisplayable);
        }
    }

    /**
     * Called by a system to indicated that a command has been invoked on a particular displayable.
     * @param command the Command that was invoked
     * @param displayable the Displayable where the command was invoked
     */
    public void commandAction(Command command, Displayable displayable) {
        if (displayable == form) {
            if (command == exitCommand) {
                exitMIDlet();
            }
			else if (command == List.SELECT_COMMAND) {
				int index = form.getSelectedIndex();
				if (index > -1)
					((AbstractUnit) units.elementAt(index)).start();
			}
        }
    }

	public Command getNextCommand() {
		if (nextCommand == null)
            nextCommand = new Command("Next", Command.ITEM, 0);

		return nextCommand;
	}

	public Command getCancelCommand() {
		if (cancelCommand == null)
            cancelCommand = new Command("Cancel", Command.CANCEL, 0);

		return cancelCommand;
	}

    /**
     * Returns an initiliazed instance of exitCommand component.
     * @return the initialized component instance
     */
    public Command getExitCommand() {
        if (exitCommand == null) {
			// write pre-init user code here
            exitCommand = new Command("Exit", Command.EXIT, 0);
			// write post-init user code here
        }
        return exitCommand;
    }

    /**
     * Returns an initiliazed instance of form component.
     * @return the initialized component instance
     */
    public Screen getMainScreen() {
        if (form == null) {
			// write pre-init user code here
			form = new List("Tutor", List.IMPLICIT);
			addUnit(new ArithmeticUnit(this, new AddQuestion()));
			addUnit(new ArithmeticUnit(this, new SubQuestion()));
			addUnit(new ArithmeticUnit(this, new MulQuestion()));
			addUnit(new ArithmeticUnit(this, new DivQuestion()));

            form.addCommand(getExitCommand());
            form.setCommandListener(this);
			// write post-init user code here

//			form.addCommand(new Command("BACK", Command.BACK, 1));
//			form.addCommand(new Command("CANCEL", Command.CANCEL, 1));
//			form.addCommand(new Command("EXIT", Command.EXIT, 1));
//			form.addCommand(new Command("HELP", Command.HELP, 1));
//			form.addCommand(new Command("ITEM", Command.ITEM, 1));
//			form.addCommand(new Command("OK", Command.OK, 1));
//			form.addCommand(new Command("SCREEN", Command.SCREEN, 1));
//			form.addCommand(new Command("STOP", Command.STOP, 1));
        }
        return form;
    }

	private void addUnit(AbstractUnit unit) {
		units.addElement(unit);
		form.append(unit.getTitle(), null);
	}

	/**
	 * Returns a display instance.
	 * @return the display instance.
	 */
	public Display getDisplay() {
		return Display.getDisplay(this);
	}

	/**
	 * Exits MIDlet.
	 */
	public void exitMIDlet() {
		switchDisplayable(null, null);
		destroyApp(true);
		notifyDestroyed();
	}

	/**
	 * Called when MIDlet is started.
	 * Checks whether the MIDlet have been already started and initialize/starts or resumes the MIDlet.
	 */
	public void startApp() {
		if (midletPaused) {
			resumeMIDlet();
		} else {
			initialize();
			startMIDlet();
		}
		midletPaused = false;
	}

	/**
	 * Called when MIDlet is paused.
	 */
	public void pauseApp() {
		midletPaused = true;
	}

	/**
	 * Called to signal the MIDlet to terminate.
	 * @param unconditional if true, then the MIDlet has to be unconditionally terminated and all resources has to be released.
	 */
	public void destroyApp(boolean unconditional) {
	}
}
